package com.itheima.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.common.exception.NcException;
import com.itheima.common.exception.enums.ResponseEnum;
import com.itheima.common.threadlocal.UserHolder;
import com.itheima.common.util.BeanHelper;
import com.itheima.sys.dto.CompanyContactConfigDTO;
import com.itheima.sys.entity.CompanyContactConfig;
import com.itheima.sys.mapper.CompanyContactConfigMapper;
import com.itheima.sys.service.ICompanyContactConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itheima
 * @since 2020-12-13
 */
@Service
public class CompanyContactConfigServiceImpl extends ServiceImpl<CompanyContactConfigMapper, CompanyContactConfig> implements ICompanyContactConfigService {

    @Override
    @Transactional
    public List<CompanyContactConfigDTO> queryOrInitContactConfig() {
        Long companyId = UserHolder.getCompanyId();
        //现根据企业ID查询通讯录信息项 数量 判断是否需要初始化
        QueryWrapper<CompanyContactConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CompanyContactConfig::getCompanyId, companyId);
        int count = this.count(queryWrapper);
        if(count==0){
            //如果数量为0 将一些固定通讯录信息项初始化到数据库中
            //没有初始化
            List<CompanyContactConfig> returnList = new ArrayList<>();

            String[] items = new String[]{"部门", "职位", "姓名", "工号", "手机号", "邮箱", "座机", "办公地点", "备注", "入职时间"};
            for (String name : items) {
                CompanyContactConfig companyContactConfig = new CompanyContactConfig();
                companyContactConfig.setType("fixed");  //固定字段
                companyContactConfig.setName(name);
                companyContactConfig.setStatus(1);  //状态
                companyContactConfig.setFieldType("string");//类型
                companyContactConfig.setCompanyId(companyId);
                returnList.add(companyContactConfig);
            }
            boolean b = this.saveBatch(returnList);
            if (!b) {
                throw new NcException(ResponseEnum.INSERT_OPERATION_FAIL);
            }
        }
        //反之直接查询列表 显示信息项
        List<CompanyContactConfig> companyContactConfigs = this.list(queryWrapper);
        return BeanHelper.copyWithCollection(companyContactConfigs, CompanyContactConfigDTO.class);
    }


    /**
     * 新增动态通讯录字段
     * @param companyContactConfigDTO
     */
    public void saveContactConfig(CompanyContactConfigDTO companyContactConfigDTO) {
        CompanyContactConfig companyContactConfig = BeanHelper.copyProperties(companyContactConfigDTO, CompanyContactConfig.class);
        companyContactConfig.setCompanyId(UserHolder.getCompanyId());
        companyContactConfig.setFieldType("string");
        companyContactConfig.setStatus(0);
        companyContactConfig.setType("dynamic");
        boolean b = this.save(companyContactConfig);
        if (!b) {
            throw new NcException(ResponseEnum.INSERT_OPERATION_FAIL);
        }
    }

    /**
     * 修改通讯录
     *
     * @param companyContactConfigDTO
     * @throws Exception
     */
    @Transactional
    public void updateContactConfig(CompanyContactConfigDTO companyContactConfigDTO) {
        CompanyContactConfig companyContactConfig = BeanHelper.copyProperties(companyContactConfigDTO, CompanyContactConfig.class);
        Long companyId = UserHolder.getCompanyId();
        companyContactConfig.setCompanyId(companyId);
        boolean b = this.updateById(companyContactConfig);
        if (!b) {
            throw new NcException(ResponseEnum.INSERT_OPERATION_FAIL);
        }
    }
}
